<?php
/**
 * 逻辑层基础类
 * @author maniac
 */
namespace app\common\logic;

use app\facade\UserLogs;

class Base
{
    /**
     * 添加/编辑逻辑处理
     * @param array $data 请求的数据
     */
    public function save($data)
    {
        // 验证器验证
        $validateResult = validate_rule($data);
        if ($validateResult !== true) exception($validateResult);
        
        $model = $this->getModel();
        
        if (isset($data['id']) && $data['id']) {
            // 修改
            $object = $model::update($data);
            $this->insertLog($model, $data);
            return true;
        } else {
            // 添加
            $object = $model::create($data, true);
            if ($object->id) {
                $data['last_id'] = $object->id;
                $this->insertLog($model, $data);
                return $object->id;
            }
        }
        return false;
    }
    
    /**
     * 批量排序
     * @param  array $data 请求的信息
     */
    public function listOrder($data)
    {
        if (isset($data['list_order']) && !empty($data['list_order'])) {
            $update = [];
            foreach ($data['list_order'] as $k => $v) {
                $update[] = [
                    'id'         => $k,
                    'list_order' => $v,
                ];
            }
            $class = $this->getModel();
            $model = new $class;
            if ($model->saveAll($update)) {
                UserLogs::insertLog(['title' => '批量排序'.$model->tableName]);
                return true;
            }
        }
        return false;
    }
    
    /**
     * 列表页修改状态
     * @param  array $data 请求的信息
     */
    public function status($data)
    {
        $model  = $this->getModel();
        $update = $this->statusAttach($data);
        $model::update($update);
        UserLogs::insertLog(['title' => '修改'.(new $model)->tableName.'状态', 'content' => "id={$data['id']}"]);
        return true;
    }
    
    /**
     * 修改状态附件
     * @param  array $data 请求的信息
     */
    public function statusAttach($data)
    {
        return [
            'id'         => $data['id'],
            $data['key'] => $data['value'] ? 0 : 1,
        ];
    }
    
    /**
     * 删除
     * @param  array $data 请求的信息
     */
    public function delete($data)
    {
        // 验证器验证
        $validateResult = validate_rule($data, 'del');
        if ($validateResult !== true) exception($validateResult);
        if (!isset($data['id']) || !$data['id']) {
            exception('请选择要删除的信息');
        }
        $model = $this->getModel();
        $obj   = new $model;
        if (property_exists($obj, 'softId') && $obj->softId) {
            $model::destroy($data['id']);
        } else {
            $model::destroy($data['id'], true);
        }
        $id = is_array($data['id']) ? implode(',', $data['id']) : $data['id'];
        UserLogs::insertLog(['title' => '删除'.$obj->tableName, 'content' => "id=".$id]);
        return true;
    }
    
    /**
     * 恢复信息
     * @param  array $data 请求的信息
     */
    public function recovery($data)
    {
        if (!isset($data['id']) || !$data['id']) {
            exception('请选择要恢复的信息');
        }
        $where = [
            ['id', 'in', $data['id']],
        ];
        $model = $this->getModel();
        $data  = $model::onlyTrashed()->where($where)->select();
        foreach ($data as $v) {
            $v->restore();
        }
    }
    
    /**
     * 获取模型
     */
    public function getModel()
    {
        return '\app\common\model\\' . request()->controller();
    }
    
    /**
     * 重组请求信息
     * @param  array $data 请求的信息
     * @return array
     */
    public function recombine($data)
    {
        return $data;
    }
    
    /**
     * 添加日志
     * @param  object $model 对象模型
     */
    public function insertLog($model, $data)
    {
        $flag    = isset($data['last_id']) && $data['last_id'];
        $object  = new $model;
        $title   = ($flag ? '添加' : '修改') . $object->tableName;
        $content = '';
        foreach ($object->logId as $v) {
            if ($v == 'id') {
                $id = $flag ? $data['last_id'] : $data['id'];
                $content .= "id={$id}\r\n";
            } else {
                if (isset($data[$v]) && $data[$v]) {
                    $content .= "{$v}={$data[$v]}\r\n";
                }
            }
        }
        $log = [
            'title'   => $title,
            'content' => $content,
        ];
        UserLogs::insertLog($log);
    }
}
